package com.rep.core.parse;

import com.rep.core.parse.model.Tables;
import lombok.extern.slf4j.Slf4j;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.util.Optional;

/**
 * @author wangye
 * @classname ReplicationParse
 * @date 2020/9/25 15:54
 **/
@Slf4j
public class DefaultReplicationParse implements ReplicationParse {


    public DefaultReplicationParse() {

    }

    @Override
    public Tables parse(String fileContent) throws Exception {
        Tables tables;
        if (fileContent == null) return null;
        JAXBContext context = JAXBContext.newInstance(Tables.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        Object unmarshal = unmarshaller.unmarshal(new StringReader(fileContent));
        tables = Optional.ofNullable(unmarshal).map(u -> (Tables) u).orElse(null);
        return tables;
    }


}
